 package com.zingrow.web.configure.iformation.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zingrow.common.exception.NumberErrorException;
import com.zingrow.common.utils.PageView;
import com.zingrow.web.configure.iformation.mapper.IformationMapper;
import com.zingrow.web.configure.iformation.model.Iformation;
import com.zingrow.web.configure.iformation.service.IIformationService;
import com.zingrow.web.customer.mapper.GroupSchemeMapper;
import com.zingrow.web.report.service.IRptStatisticsService;
import com.zingrow.web.user.mapper.UserPermissionMapper;



@Service
public class IformationService implements IIformationService{

    @Autowired
    private IformationMapper iformationMapper;
    @Autowired
    private GroupSchemeMapper groupSchemeMapper; 
    @Autowired
    private UserPermissionMapper userPermissionMapper;
    @Lazy(true)
    @Autowired
	private IRptStatisticsService rptStatisticsService;
    
    /**
     * 通过方案id查询方案名称
     * 
     * */
    @Override
    public String ifmNamebyifmId(String ifmId){
    	return iformationMapper.ifmNamebyifmId(ifmId);
    }
    
    /**
     * 通过方案id查询类别名称
     * APP操作日志使用
     */
    @Override
    public String appselectbyifmId(String ifmId){
    	return iformationMapper.appselectbyifmId(ifmId);
    }
    
    /**
     *通过方案id查询组织id 
     * */
    @Override
    public Integer selectGroupId(Integer ifm_id){
    	return iformationMapper.selectGroupId(ifm_id);
    }

    //条件模糊分页查询
    @Override
    public List<Iformation> queryByParams(PageView pageView, String ifm_name,String ifm_note,Integer groupId
    		,String ifm_isdelete,String ifm_categoryName) {
        Map<String, Object> map = new HashMap<>();
        map.put("paging", pageView);
        map.put("ifm_name", ifm_name);
        map.put("ifm_note", ifm_note);
        map.put("groupId", groupId);
        map.put("ifm_isdelete", ifm_isdelete);
        map.put("ifm_categoryName", ifm_categoryName);
        return iformationMapper.queryByParams(map);
    }

    //新增
    @Override
    public boolean addIformation(Iformation iformation) {
        // 默认不推送
        if (null == iformation.getIfm_pushed()) {
            iformation.setIfm_pushed(0);
        }
        // 默认APP
        
        if (iformation.getIfm_pushed().equals(1) && null == iformation.getIfm_type()) {
            iformation.setIfm_type("app");
        }
        //默认关闭
        if (null == iformation.getIfm_isdelete()) {
            iformation.setIfm_isdelete(0);
        }
        //默认非敏感
        if(null == iformation.getIfm_isSensitivity()) {
        	iformation.setIfm_isSensitivity(0);
        }
        return iformationMapper.addIformation(iformation) > 0;
    }
    //通过类别名称查询类别
    @Override
    public Iformation selectNameBycategory(Iformation iformation){
    	
    	return iformationMapper.selectNameBycategory(iformation);
    }

    //修改1
    @Override
    public boolean modifyIformation(Iformation iformation) {
       return iformationMapper.modifyIformation(iformation) > 0;
    }
  //修改2
    @Override
    public boolean modifyIformation2(Iformation iformation, String oldCategory) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("oldCategory", oldCategory);
    	map.put("ifm_category", iformation.getIfm_category());
    	map.put("ifm_categoryName", iformation.getIfm_categoryName());
    	map.put("groupId", iformation.getGroupId());
       return iformationMapper.modifyIformation2(map) > 0;
    }
  //修改3
    @Override
    public boolean modifyIformation3(Iformation iformation) {
    	return iformationMapper.modifyIformation3(iformation) > 0;
    }
    /*
     * * 根据主键修改信息4—— 根据方案id组织id类别修改类别名称-->
     */
  
    @Override
    public boolean modifyIformation4(Iformation iformation, String oldCategory) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("oldCategory", oldCategory);
    	map.put("ifm_category", iformation.getIfm_category());
    	map.put("ifm_categoryName", iformation.getIfm_categoryName());
    	map.put("groupId", iformation.getGroupId());
    	map.put("ifm_id", iformation.getIfm_id());
       return iformationMapper.modifyIformation4(map) > 0;
    }
    
    
    //查询同名
    @Override
    public boolean querySameByifmName(String ifm_name, Integer ifm_id) {
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("ifm_name", ifm_name);
    	map.put("ifm_id", ifm_id);
        return iformationMapper.selectSameByifmName(map) > 0;
    }
    @Override
    public List<Iformation> selectCategoryNameByGroupid(Integer groupId){
    	return  iformationMapper.selectCategoryNameByGroupid(groupId);
    }
    /*
     * 判断同一个组织内同一个类别只允许一个类别名称
     * 
     * */
    @Override
    public Iformation selectIfm_categoryNameByGroupId(Integer groupId,String ifm_category){
    	Map<String, Object> map = new HashMap<String, Object>();
    	map.put("group_id", groupId);
    	map.put("ifm_category", ifm_category);
    	return iformationMapper.selectIfm_categoryNameByGroupId(map) ;
    			
    }

  //单个查询1
    @Override
    public Iformation queryByID(Integer ifm_id) {
        return iformationMapper.queryByID(ifm_id);
    }
    
    //单个查询2
    @Override
    public Iformation queryByID2(Integer ifm_id,Integer group_id) {
        return iformationMapper.queryByID2(ifm_id, group_id);
    }

    
    
    //删除
    @Override
    @Transactional
    public boolean deleteByid(Integer ifm_id) throws NumberErrorException{
    	Map<Integer, Date> map = new HashMap<Integer, Date>();
    	//删除方案，删除政府版中该方案和组织的关系
    	groupSchemeMapper.delSchemeifmId(ifm_id);
    	//删除方案，根据方案ID删除政府版中该方案和用户的关系
    	userPermissionMapper.deluserscheme(ifm_id);
    	//根据方案ID删除模板模块表
    	//rptModuleInfoMapper.deleteplanId(ifm_id);
    	//删除方案，删除组织方案关系表
    	groupSchemeMapper.delGroupSchemeifmId(ifm_id);
    	map.put(ifm_id, null);
    	rptStatisticsService.deleteStatistics(map);
    	return iformationMapper.deleteByid(ifm_id)>0;
    }

    @Override
    public List<Iformation> queryIformationByUserName(String user_account) {
        return iformationMapper.queryIformationByUserName(user_account);
    }
    
    //条件模糊分页查询--组织接口
    @Override
    public List<Iformation> queryByParamsNN(PageView pageView, String ifm_name,String ifm_note,int gid) {
        Map<String, Object> map = new HashMap<>();
        if (null != pageView) {
        map.put("paging", pageView);
        }
        map.put("ifm_name", ifm_name);
        map.put("ifm_note", ifm_note);
        map.put("group_id", gid);
        return iformationMapper.queryByParamsNN(map);
    }
    
    /**
     * 条件模糊分页查询--组织接口
     * @param pageView
     * @param ifm_name
     * @param ifm_note
     * @return
     */
    @Override
    public List<Iformation> queryBymg(PageView pageView, String ifm_name,String ifm_note) {
        Map<String, Object> map = new HashMap<>();
        map.put("paging", pageView);
        map.put("ifm_name", ifm_name);
        map.put("ifm_note", ifm_note);
        return iformationMapper.queryBymg(map);
    }
    /**
     * 根据方案id查询方案名称  app接口
     * @param ids
     * @return
     */
    @Override
    public List<Iformation> selectId(List<String> ids) {
    	return iformationMapper.selectId(ids);
    }
    
    @Override
    public List<Iformation> selectlistId(List<String> ids) {
    	return iformationMapper.selectlistId(ids);
    }

	@Override
	public List<Integer> getAllId() {
		return iformationMapper.getAllId();
	}
	 /**
     * 根据信息查询当前新增方案ID
     * @return
     */
	@Override
	public Iformation selectbyname(String ifm_name,String ifm_note,String ifm_getKeyword) {
		return iformationMapper.selectbyname(ifm_name,ifm_note,ifm_getKeyword);
	}

	public Iformation selectbyifmid(int ifm_id)
	{
		return iformationMapper.selectgetchannel(ifm_id);
	};
	public List<Iformation> selectcheckalready(int gid)
	{
		return iformationMapper.selectcheck(gid);
	};
	
}
